import tkinter as tk
from tkinter import simpledialog, messagebox, ttk


class DrawingDialog(simpledialog.Dialog):
    def body(self, master):
        self.title("Drawing Shape")

        # Shape selection
        tk.Label(master, text="Shape:").grid(row=0)
        self.shape_var = tk.StringVar(value="Line")
        self.shape_option = ttk.Combobox(
            master, textvariable=self.shape_var, values=["Line", "Rectangle"]
        )
        self.shape_option.grid(row=0, column=1)

        # Position 1
        tk.Label(master, text="Position 1:").grid(row=1)
        self.row1_var = tk.StringVar(value="1")
        self.row1_option = ttk.Combobox(
            master, textvariable=self.row1_var, values=list(map(str, range(1, 10)))
        )
        self.row1_option.grid(row=1, column=1)
        self.col1_var = tk.StringVar(value="1")
        self.col1_option = ttk.Combobox(
            master, textvariable=self.col1_var, values=list(map(str, range(1, 10)))
        )
        self.col1_option.grid(row=1, column=2)
        self.cand1_var = tk.StringVar(value="1")
        self.cand1_option = ttk.Combobox(
            master, textvariable=self.cand1_var, values=list(map(str, range(1, 10)))
        )
        self.cand1_option.grid(row=1, column=3)

        # Position 2
        tk.Label(master, text="Position 2:").grid(row=2)
        self.row2_var = tk.StringVar(value="1")
        self.row2_option = ttk.Combobox(
            master, textvariable=self.row2_var, values=list(map(str, range(1, 10)))
        )
        self.row2_option.grid(row=2, column=1)
        self.col2_var = tk.StringVar(value="1")
        self.col2_option = ttk.Combobox(
            master, textvariable=self.col2_var, values=list(map(str, range(1, 10)))
        )
        self.col2_option.grid(row=2, column=2)
        self.cand2_var = tk.StringVar(value="1")
        self.cand2_option = ttk.Combobox(
            master, textvariable=self.cand2_var, values=list(map(str, range(1, 10)))
        )
        self.cand2_option.grid(row=2, column=3)

        # Line width
        tk.Label(master, text="Line Width:").grid(row=3)
        self.linewidth_var = tk.Entry(master)
        self.linewidth_var.insert(0, "3")  # Set default value to 2
        self.linewidth_var.grid(row=3, column=1)

        # Color
        tk.Label(master, text="Color (RGB):").grid(row=4)
        self.color_var = tk.Entry(master)
        self.color_var.insert(0, "FF0000")  # Set default value to FF0000
        self.color_var.grid(row=4, column=1)

        # Line type
        tk.Label(master, text="Line Type:").grid(row=5)
        self.linetype_var = tk.StringVar(value="Solid")
        self.linetype_option = ttk.Combobox(
            master, textvariable=self.linetype_var, values=["Solid", "Dashed"]
        )
        self.linetype_option.grid(row=5, column=1)

        # Has arrow
        tk.Label(master, text="Has Arrow:").grid(row=6)
        self.arrow_var = tk.BooleanVar()
        self.arrow_checkbutton = tk.Checkbutton(master, variable=self.arrow_var)
        self.arrow_checkbutton.grid(row=6, column=1)

        return self.shape_option  # initial focus

    def apply(self):
        self.result = {
            "shape": self.shape_var.get(),
            "position1": (
                self.row1_var.get(),
                self.col1_var.get(),
                self.cand1_var.get(),
            ),
            "position2": (
                self.row2_var.get(),
                self.col2_var.get(),
                self.cand2_var.get(),
            ),
            "linewidth": self.linewidth_var.get(),
            "color": self.color_var.get(),
            "linetype": self.linetype_var.get(),
            "arrow": self.arrow_var.get(),
        }


class ChainDialog(simpledialog.Dialog):
    def __init__(self, parent, title=None, colors=None):
        self.colors = colors if colors else ["#00FF00", "#0000FF", "#FF0000"]
        # self.selected_canvas = None
        super().__init__(parent, title)

    def body(self, master):
        tk.Label(master, text="Shape:").grid(row=0, sticky="w", pady=(5, 0))
        self.shape = tk.StringVar(value="chain")

        shapes = ["cand", "cell", "digit", "chain"]
        shape_option = tk.OptionMenu(master, self.shape, *shapes)
        shape_option.grid(row=0, column=1, sticky="w", pady=(5, 0))

        tk.Label(master, text="Description:").grid(row=1, sticky="w", pady=(5, 0))
        self.description = tk.Text(master, width=50, height=3)
        self.description.grid(row=1, column=1, pady=(5, 0))

        self.color_label = tk.Label(master, text="Color:", foreground=self.colors[0])
        self.color_label.grid(row=2, sticky="w", pady=(5, 0))
        # self.color_label.config(foreground=self.colors[0])

        self.color = tk.StringVar(value=self.colors[0])
        color_option = tk.OptionMenu(master, self.color, *self.colors)
        color_option.grid(row=2, column=1, sticky="w", pady=(5, 0))

        self.color.trace_add("write", self.color_changed)

        return self.description

    def color_changed(self, *args):
        selected_color = self.color.get()
        # print(self.colors, self.color_label)
        self.color_label.config(foreground=selected_color)

    def apply(self):
        # 用户点击确定按钮时调用此方法
        shape = self.shape.get()
        description = self.description.get("1.0", tk.END).strip()
        color = self.color.get()
        self.result = (shape, description, color)


def open_drawing_dialog():
    dialog = DrawingDialog(root)
    if dialog.result:
        messagebox.showinfo(
            "Result", f"You selected the following parameters:\n{dialog.result}"
        )


def open_chain_dialog():
    colors = [
        "#0096FF",
        "#00FA92",
        "#FF9300",
        "#FF40FF",
        "#0355F1",
        "#00F900",
        "#945200",
        "#9437FF",
    ]

    dialog = ChainDialog(root, colors=colors)
    if dialog.result:
        shape, description, color = dialog.result
        messagebox.showinfo("Result", f"{shape}\n{description}\n{color}")


if __name__ == "__main__":
    root = tk.Tk()
    root.title("Drawing Tool Example")
    root.geometry("400x300")

    button = tk.Button(root, text="Open Drawing Dialog", command=open_chain_dialog)
    button.pack(pady=50)

    root.mainloop()
